<?php

namespace App\Controller\LazyCommon;

use App\Model\LazyCommon\AdminMenuModel;
use App\Model\LazyCommon\RoleModel;
use App\Model\LazyCommon\UserGroupsModel;
use App\Model\LazyCommon\AdminMenuUserModel;
use App\Setting\LazyCommon\AdminMenuUserSetting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Entity\AdminMenuUser  as Entity;

/**
 * @route("/admin/")
 */
class AdminMenuUserAdminController extends BaseController
{

    /**
     * @route("AdminMenuUser/add",name="AdminMenuUserPreAdd")
     */
    public function add()
    {
        //通用代码
        $this->setSetting(new AdminMenuUserSetting($this->getDoctrine()->getManager()));
        return $this->comAdd();
    }

    /**
     * @route("AdminMenuUser/add/do",name="AdminMenuUserDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //通用代码

        if(!$form = $this->getParamRequired("form")){return $this->msg_out("参数缺失");}
        $menus = $form["menu"];
        $user = $form["user"];

        $AdminMenuUserModel = new AdminMenuUserModel($this->getDoctrine());
        $AdminMenuUserModel->update($user,$menus);
        return $this->json_out(1,"添加成功",$this->generateUrl("AdminMenuUserLists"));
    }

    /**
     * @route("AdminMenuUser/edit/{id}",name="AdminMenuUserPreEdit")
     */
    public function edit(Entity $entity)
    {
        //私有代码部分
        $AdminMenuUserModel = new AdminMenuUserModel($this->getDoctrine());
        $menu = $AdminMenuUserModel->getMenu4Groups($entity->getUser());
        $entity->setMenu($menu);
        $this->setSetting(new AdminMenuUserSetting($this->getDoctrine()->getManager()));
        return $this->comEdit($entity);
    }

    /**
     * @route("AdminMenuUser/edit/{id}/do",name="AdminMenuUserDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit()
    {

        if(!$form = $this->getParamRequired("form")){return $this->msg_out("参数缺失");}
        $menus = $form["menu"];
        $user = $form["user"];

        $AdminMenuUserModel = new AdminMenuUserModel($this->getDoctrine());
        $AdminMenuUserModel->update($user,$menus);
        return $this->json_out(1,"添加成功",$this->generateUrl("AdminMenuUserLists"));
    }

    /**
     * @route("AdminMenuUser",name="AdminMenuUserLists")
     */
    public function lists()
    {
        //私有部分
        $UserGroupsModel = new UserGroupsModel($this->getDoctrine());
        $UserGroups = $UserGroupsModel->getAllUserGroupsTree();
        $AdminMenuModel = new AdminMenuModel($this->getDoctrine());
        $Menu = $AdminMenuModel->getAllAdminMenuTree();
        $tools = array(
            "groups"    => $UserGroups,
            "menu"      => $Menu,
        );

        //通用部分
        $this->setSetting(new AdminMenuUserSetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("AdminMenuUser/tools",name="AdminMenuUserTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new AdminMenuUserSetting($this->getDoctrine()->getManager()));
        return $this->comTools($request);
    }

}
